<?
  class InputCheck extends Input
  {
    var $aData;
    var $bMultiple = false;
	var $cLanguage = '';
	var $nqColumns = 3;
	var $bAutoSort= true;

function setAutoSort( $bAutoSort )
	{
		$this->bAutoSort= $bAutoSort;
	}
	
function setNumberOfColumns( $nqColumns)
	{
		$this->nqColumns = $nqColumns;
	}
	
function getValueString( $cSeparador )
{
	return implode($cSeparador, $this->cValue );
}
function setLanguage( $cLanguage)
	{
		$this->cLanguage = $cLanguage;
	}
  function setMultiple( $bMultipleSelecction = true )
    {
      $this->bMultiple = $bMultipleSelecction;
    }
  function setMatrix ( $aData )
    {
      $this->aData = $aData;
    }
  function InputCheck( $cLabel, $cField, $cValue = '', $bMandatory = false)
    {
      $this->Input( $cLabel, $cField, $cValue, $bMandatory );
      $this->bMultiple = false;
    }
	function setValueArray($aValues)
	{
		for( $nIndex = 0; $nIndex < count($this->aData); $nIndex++)
		{
			$this->aData[$nIndex][2] = in_array($this->aData[$nIndex][0], $aValues) ? 1: 0;
		}
	}
  function addOption( $cDataValue, $cDataDescription, $bValue = false, $cGroup = ''  )
  {
    $this->aData[] = array ( $cDataValue, $cDataDescription, $bValue, $cGroup );
  }
  function makeItem( $cDataValue, $cDataDescription = '' )
    {
      $cReturn = "";
      $cChecked = ( $this->cValue == $cDataValue && strlen($this->cValue) > 0) ? " checked=\"checked\" ": "";
      $cReturn .= "$cDataDescription&nbsp;";
      $cType =  ( $this->bMultiple ) ? 'checkbox' : 'radio';
      $cReturn .= "<input type='$cType' $cChecked id='". $this->cField . "' name='". $this->cField . "' value='$cDataValue'/>";
      return $cReturn;
    }
  function make()
    {
		// Si esta setea como Oculto
			if ( $this->bHidden )  return parent::make();
		if ( $this->bMultiple  )
		  {
			return "<div style='position: relative;'>" . $this->makeCheck() ."</div><div style='clear: both;'></div>";;
		  } else
		  {
			return $this->makeRadio();
		  }
    }
  function makeRadio()
    {
      $cReturn = "";
      if ( $this->bMandatory && $this->cValue === "" )
      {
        $this->cValue = $this->aData[0][1];
      }
		$cPropiedades = $this->getProperties();	  
      foreach ($this->aData as $aFila)
        {
			$cDataValue       = $aFila[0];
			$cDataDescription = $aFila[1];
			if ( $this->cLanguage ) $cDataDescription  = extractLanguage( $cDataDescription  , $this->cLanguage );
			$cGroup = $aFila[3];
			$cChecked = ( $this->cValue == $cDataValue && strlen($this->cValue) > 0) ? " checked=\"checked\" ": "";
			$cReturn .= "<span class='input_check'  onclick='javascript: oInput = document.getElementById( \"" . $this->cField . "_" . $cDataValue . "\" ); oInput.checked= true; if (oInput.onchange) oInput.onchange(); return false;'>$cDataDescription</span>";
			$cReturn .= "<input type='radio' $cPropiedades  $cChecked id='" . $this->cField . "_" . $cDataValue . "'  name='". $this->cField . "' value='$cDataValue'/>";
        }
        return $cReturn;
    }
  function showLabel( $cLabel = '', $cAccessKey = '' )
	{
		$cReturn = parent::showLabel( $cLabel , $cAccessKey);
		  if ( $this->getFormName() ) $cReturn .= "
					<a href='javascript: void(0)' onclick='javascript: checkAll(  \"" . $this->getFormName() . "\", \"" . $this->cField. "\", true );'>[Seleccionar]</a>
					<a href='javascript: void(0)' onclick='javascript: checkAll(  \"" . $this->getFormName() . "\", \"" . $this->cField. "\", false );'>[Deseleccionar]</a>
					<a href='javascript: void(0)' onclick='javascript: invertCheck(  \"" . $this->getFormName() . "\", \"" . $this->cField. "\" );'>[Invertir]</a>";
        return $cReturn . '<br/>';
	}

  function makeCheck()
    {
      $cReturn = "";
	  $cGroupAnterior  = "";
	  if ( $this->bAutoSort ) usort( $this->aData , "inputCheckCompare");
	  $cArrayControl = count($this->aData) > 1 ? '[]':'';
		if ( $this->nqColumns ) 
		{
			$cInicioDiv = "<div style='float: left; width:" . (99 / $this->nqColumns) . "%' >";
			$cFinDiv = "</div>";
		}

      foreach ($this->aData as $aFila)
      {
        $cDataValue       = $aFila[0];
        $cDataDescription = $aFila[1];
		if ( $this->cLanguage ) $cDataDescription  = extractLanguage( $cDataDescription  , $this->cLanguage );
		$cGroup = $aFila[3];
        $cChecked = $aFila[2] == true ||  ( is_array($this->cValue) &&  in_array($cDataValue, $this->cValue)  ) ? " checked=\"checked\" ": "";
		if ( $cGroupAnterior != $cGroup  )
		{
			if ( $cGroupAnterior ) $cReturn.= "</div>";
			$cReturn .= "<div style='float: left; width: 100%'><span class='check_title'>$cGroup</span>";
			$cGroupAnterior = $cGroup ;
		}
		$cPropiedades = $this->getProperties();
		$cReturn .= $cInicioDiv;
        $cReturn .= "<input $cPropiedades  type='checkbox' $cChecked id='" . $this->cField . "_" . $cDataValue . "'  name='". $this->cField . "$cArrayControl' value='$cDataValue'/>";
		$cReturn .= "&nbsp;<span class='input_check'  onclick='javascript: oInput = document.getElementById( \"" . $this->cField . "_" . $cDataValue . "\" ); oInput.checked= oInput.checked ? false:true; if (oInput.onchange) oInput.onchange(); return false;'>$cDataDescription</span>";
		$cReturn .= $cFinDiv;
      }
      if ( $cGroupAnterior )$cReturn .= "</div>";
      return $cReturn;
    }
  }
function inputCheckCompare($a, $b)
{
	if ($a[3] == $b[3])
	{
		if ($a[1] == $b[1])
			return 0;
		else
			return ($a[1] < $b[1]) ? -1 : 1;
	}
	return ($a[3] < $b[3]) ? -1 : 1;
}

?>
